/**
 * 本地存储工具类
 */
class Storage {
  private prefix: string

  constructor(prefix = 'test_platform_') {
    this.prefix = prefix
  }

  /**
   * 获取完整的键名
   */
  private getKey(key: string): string {
    return `${this.prefix}${key}`
  }

  /**
   * 设置存储项
   */
  set<T>(key: string, value: T): void {
    try {
      const serializedValue = JSON.stringify(value)
      localStorage.setItem(this.getKey(key), serializedValue)
    } catch (error) {
      console.error(`Failed to set localStorage item ${key}:`, error)
    }
  }

  /**
   * 获取存储项
   */
  get<T>(key: string, defaultValue?: T): T | null {
    try {
      const item = localStorage.getItem(this.getKey(key))
      if (item === null) {
        return defaultValue ?? null
      }
      return JSON.parse(item)
    } catch (error) {
      console.error(`Failed to get localStorage item ${key}:`, error)
      return defaultValue ?? null
    }
  }

  /**
   * 移除存储项
   */
  remove(key: string): void {
    try {
      localStorage.removeItem(this.getKey(key))
    } catch (error) {
      console.error(`Failed to remove localStorage item ${key}:`, error)
    }
  }

  /**
   * 清空所有存储项
   */
  clear(): void {
    try {
      const keysToRemove: string[] = []
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i)
        if (key && key.startsWith(this.prefix)) {
          keysToRemove.push(key)
        }
      }
      keysToRemove.forEach(key => localStorage.removeItem(key))
    } catch (error) {
      console.error('Failed to clear localStorage:', error)
    }
  }

  /**
   * 检查是否存在某个键
   */
  exists(key: string): boolean {
    return localStorage.getItem(this.getKey(key)) !== null
  }

  /**
   * 获取所有键
   */
  getKeys(): string[] {
    const keys: string[] = []
    try {
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i)
        if (key && key.startsWith(this.prefix)) {
          keys.push(key.replace(this.prefix, ''))
        }
      }
    } catch (error) {
      console.error('Failed to get localStorage keys:', error)
    }
    return keys
  }
}

// 导出单例实例
export const storage = new Storage()

// 会话存储工具
class SessionStorage {
  private prefix: string

  constructor(prefix = 'test_platform_session_') {
    this.prefix = prefix
  }

  private getKey(key: string): string {
    return `${this.prefix}${key}`
  }

  set<T>(key: string, value: T): void {
    try {
      const serializedValue = JSON.stringify(value)
      sessionStorage.setItem(this.getKey(key), serializedValue)
    } catch (error) {
      console.error(`Failed to set sessionStorage item ${key}:`, error)
    }
  }

  get<T>(key: string, defaultValue?: T): T | null {
    try {
      const item = sessionStorage.getItem(this.getKey(key))
      if (item === null) {
        return defaultValue ?? null
      }
      return JSON.parse(item)
    } catch (error) {
      console.error(`Failed to get sessionStorage item ${key}:`, error)
      return defaultValue ?? null
    }
  }

  remove(key: string): void {
    try {
      sessionStorage.removeItem(this.getKey(key))
    } catch (error) {
      console.error(`Failed to remove sessionStorage item ${key}:`, error)
    }
  }

  clear(): void {
    try {
      const keysToRemove: string[] = []
      for (let i = 0; i < sessionStorage.length; i++) {
        const key = sessionStorage.key(i)
        if (key && key.startsWith(this.prefix)) {
          keysToRemove.push(key)
        }
      }
      keysToRemove.forEach(key => sessionStorage.removeItem(key))
    } catch (error) {
      console.error('Failed to clear sessionStorage:', error)
    }
  }
}

export const sessionStore = new SessionStorage()

